<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!--      <el-form-item label="老师名称" prop="teacherName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.teacherName"-->
      <!--          placeholder="请输入老师名称"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="发丝加密" prop="hairEncryptionType">
        <el-select
          @change="handleQuery"
          v-model="queryParams.hairEncryptionType"
          placeholder="产品类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in hair_encryption_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="破损修复" prop="damageRepair">
        <el-select
          @change="handleQuery"
          v-model="queryParams.damageRepair"
          placeholder="产品类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in hair_damaged_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="局部换新" prop="partialRenewalType">
        <el-select
          @change="handleQuery"
          v-model="queryParams.partialRenewalType"
          placeholder="产品类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in hair_renew_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="Plus"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['system:hairAfterSale:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:hairAfterSale:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:hairAfterSale:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:hairAfterSale:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="hairAfterSaleList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="用户昵称" align="center" prop="nickname" />
      <!--      <el-table-column label="" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="老师名称" align="center" prop="teacherName" />
      <el-table-column
        label="发丝加密类型"
        align="center"
        prop="hairEncryptionType"
      >
        <template #default="scope">
          <dict-tag
            :options="hair_encryption_type"
            :value="scope.row.hairEncryptionType"
        /></template>
      </el-table-column>
      <el-table-column label="破损修复类型" align="center" prop="damageRepair">
        <template #default="scope">
          <dict-tag
            :options="hair_damaged_type"
            :value="scope.row.damageRepair"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="局部换新类型"
        align="center"
        prop="partialRenewalType"
      >
        <template #default="scope">
          <dict-tag
            :options="hair_renew_type"
            :value="scope.row.partialRenewalType"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注信息" align="center" prop="remark" />

      <!-- <el-table-column label="售后申请状态" align="center" prop="status" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:hairAfterSale:edit']"
            >查看</el-button
          >
          <el-button
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:hairAfterSale:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改极客补发小程序售后服务对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="hairAfterSaleRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="老师名称" prop="teacherName">
          <el-input
            v-model="form.teacherName"
            placeholder="请输入老师名称"
            disabled
          />
        </el-form-item>
        <el-form-item label="破损修复类型，字典形式" prop="damageRepair">
          <el-input
            disabled
            v-model="form.damageRepair"
            placeholder="请输入破损修复类型，字典形式"
          />
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input
            v-model="form.remark"
            placeholder="请输入备注信息"
            disabled
          />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" disabled />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickname">
          <el-input
            v-model="form.nickname"
            placeholder="请输入用户昵称"
            disabled
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <!-- <el-button :loading="buttonLoading" type="primary" @click="submitForm"
            >确 定</el-button
          > -->
          <el-button @click="cancel">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="HairAfterSale">
import {
  listHairAfterSale,
  getHairAfterSale,
  delHairAfterSale,
  addHairAfterSale,
  updateHairAfterSale,
} from "@/api/business/hairAfterSale";

const { proxy } = getCurrentInstance();
const { hair_encryption_type, hair_damaged_type, hair_renew_type } =
  proxy.useDict("hair_encryption_type", "hair_damaged_type", "hair_renew_type");
const hairAfterSaleList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    teacherName: undefined,
    hairEncryptionType: undefined,
    damageRepair: undefined,
    partialRenewalType: undefined,
    userId: undefined,
    nickname: undefined,
    status: undefined,
  },
  rules: {
    id: [{ required: true, message: "不能为空", trigger: "blur" }],
    teacherName: [
      { required: true, message: "老师名称不能为空", trigger: "blur" },
    ],
    hairEncryptionType: [
      {
        required: true,
        message: "发丝加密类型，字典形式不能为空",
        trigger: "change",
      },
    ],
    damageRepair: [
      {
        required: true,
        message: "破损修复类型，字典形式不能为空",
        trigger: "blur",
      },
    ],
    partialRenewalType: [
      {
        required: true,
        message: "局部换新类型，字典形式不能为空",
        trigger: "change",
      },
    ],
    remark: [{ required: true, message: "备注信息不能为空", trigger: "blur" }],
    userId: [{ required: true, message: "用户id不能为空", trigger: "blur" }],
    nickname: [
      { required: true, message: "用户昵称不能为空", trigger: "blur" },
    ],
    status: [
      { required: true, message: "售后申请状态不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询极客补发小程序售后服务列表 */
function getList() {
  loading.value = true;
  listHairAfterSale(queryParams.value).then((response) => {
    console.log(response);
    hairAfterSaleList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    teacherName: null,
    hairEncryptionType: null,
    damageRepair: null,
    partialRenewalType: null,
    remark: null,
    userId: null,
    nickname: null,
    status: null,
  };
  proxy.resetForm("hairAfterSaleRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加极客补发小程序售后服务";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true;
  reset();
  const _id = row.id || ids.value;
  getHairAfterSale(_id).then((response) => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "极客补发小程序售后服务";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["hairAfterSaleRef"].validate((valid) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateHairAfterSale(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      } else {
        addHairAfterSale(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除极客补发小程序售后服务编号为"' + _ids + '"的数据项？')
    .then(function () {
      loading.value = true;
      return delHairAfterSale(_ids);
    })
    .then(() => {
      loading.value = true;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {})
    .finally(() => {
      loading.value = false;
    });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/hairAfterSale/export",
    {
      ...queryParams.value,
    },
    `hairAfterSale_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
